package com.photoeditor.demo.ui.widget.stash.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.support.annotation.IdRes;
import android.support.v4.view.animation.PathInterpolatorCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * Created by edward on 17-6-26.
 */

public class AnimatedRadioGroup extends RadioGroup implements RadioGroup.OnCheckedChangeListener {
    private OnCheckedChangeListener mOnCheckedChangeListener;

    private View mLastSelectedView = null;

    public AnimatedRadioGroup(Context context) {
        super(context);
        init();
    }

    public AnimatedRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void init() {
        super.setOnCheckedChangeListener(this);
    }

    @Override
    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        mOnCheckedChangeListener = listener;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        RadioButton selectedView = (RadioButton) group.findViewById(checkedId);
        if (selectedView == null || !selectedView.isChecked()) {
            return;
        }
        for (int i = 0; i < group.getChildCount(); i++) {
            View view = getChildAt(i);
            view.setAnimation(null);
        }
        new SelectedAnimation(selectedView).start();
        selectedView.setScaleX(1.5f);
        selectedView.setScaleY(1.5f);
        if (mLastSelectedView != null) {
            mLastSelectedView.setScaleX(1);
            mLastSelectedView.setScaleY(1);
        }
        if (mOnCheckedChangeListener != null) {
            mOnCheckedChangeListener.onCheckedChanged(group, checkedId);
        }
        mLastSelectedView = selectedView;
    }

    public static class SelectedAnimation extends ValueAnimator {
        private View mAnimationView;

        public SelectedAnimation(View animationView) {
            mAnimationView = animationView;
            setFloatValues(1f, 1.2f);
            setDuration(200);
            setInterpolator(PathInterpolatorCompat.create(.32f, 3.5f, .17f, -0.31f));
            addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float scale = (float) animation.getAnimatedValue();
                    mAnimationView.setScaleX(scale);
                    mAnimationView.setScaleY(scale);
                }
            });
        }

        public SelectedAnimation(View animationView, float scale) {
            mAnimationView = animationView;
            setFloatValues(1f, scale);
            setDuration(200);
            setInterpolator(PathInterpolatorCompat.create(.32f, 3.5f, .17f, -0.31f));
            addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float scale = (float) animation.getAnimatedValue();
                    mAnimationView.setScaleX(scale);
                    mAnimationView.setScaleY(scale);
                }
            });
        }

        @Override
        public void start() {
            if (mAnimationView == null) {
                return;
            }
            super.start();
        }
    }

    @Override
    public void clearCheck() {
        super.clearCheck();
        if (mLastSelectedView != null) {
            mLastSelectedView.setScaleX(1);
            mLastSelectedView.setScaleY(1);
        }
    }
}
